﻿using Autofac;
using Autofac.Extras.DynamicProxy; 
using Kylin.Wiki.Core.CacheManager.IService;
using Kylin.Wiki.Core.CacheManager.Service;
using Kylin.Wiki.Core.Configuration; 
using Kylin.Wiki.Repository;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System; 
using System.IO; 
using System.Reflection; 

namespace Kylin.Wiki.Web.Common
{
    public static class AutofacContainerModuleExtension
    {
        //  private static bool _isMysql = false;
        public static IServiceCollection AddModule(this IServiceCollection services, ContainerBuilder builder, IConfiguration configuration)
        {
            //services.AddSession();
            //services.AddMemoryCache();
            //初始化配置文件
            AppSetting.Init(services, configuration);


            var basePath = AppContext.BaseDirectory;
            //builder.RegisterType<AdvertisementServices>().As<IAdvertisementServices>();


            #region 带有接口层的服务注入

            var servicesDllFile = Path.Combine(basePath, "Kylin.Wiki.Services.dll");
            var repositoryDllFile = Path.Combine(basePath, "Kylin.Wiki.Repository.dll");

            if (!(File.Exists(servicesDllFile) && File.Exists(repositoryDllFile)))
            {
                var msg = "Repository.dll和service.dll 丢失，因为项目解耦了，所以需要先F6编译，再F5运行，请检查 bin 文件夹，并拷贝。";
                throw new Exception(msg);
            }



            builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency();//注册仓储
                                                                                                            //builder.RegisterGeneric(typeof(IUnitOfWork)).As(typeof(UnitOfWork)).InstancePerDependency();//注册仓储
                                                                                                            //services.AddScoped(typeof(IUnitOfWork), typeof(UnitOfWork));
            builder.RegisterType<UnitOfWork<OnlyOfficeContext>>()
            .As<IUnitOfWork>()
            .InstancePerLifetimeScope();
            // 获取 Service.dll 程序集服务，并注册
            var assemblysServices = Assembly.LoadFrom(servicesDllFile);
            builder.RegisterAssemblyTypes(assemblysServices)
                      .AsImplementedInterfaces()
                      .InstancePerDependency()
                      .PropertiesAutowired()
                      .EnableInterfaceInterceptors();//引用Autofac.Extras.DynamicProxy;


            // 获取 Repository.dll 程序集服务，并注册
            var assemblysRepository = Assembly.LoadFrom(repositoryDllFile);
            builder.RegisterAssemblyTypes(assemblysRepository)
                   .AsImplementedInterfaces()
                   .PropertiesAutowired()
                   .InstancePerDependency();

            #endregion

            builder.RegisterType<UserContext>().InstancePerLifetimeScope();
            //builder.RegisterType<ActionObserver>().InstancePerLifetimeScope(); 
            //注册数据库基础操作和工作单元   
             

            //启用缓存
            if (AppSetting.UseRedis)
            {
                builder.RegisterType<RedisCacheService>().As<ICacheService>().SingleInstance();
            }
            else
            {
                builder.RegisterType<MemoryCacheService>().As<ICacheService>().SingleInstance();
            }
            return services;
        }

    }
 
 

}
